// pages/group/info.js
var app = getApp(),
  a = app.requirejs("core");
Page({
  data:{
    num:1,
    show:false,
    display:false
  },
  onGotUserInfo: function (q) {
    var id = this.id;
    var that = this,
      e = app.getCache("userinfo");

    if (e) {
      that.setData({
        display: false
      })
      return;
    }
    app.getUserInfo(q.detail.userInfo, function (t) {
      if (t != 1000) {
        var ucache = getApp().getCache("userinfo");
        if (ucache) {
          that.getDetail(id);
          that.setData({
            display: false,
            getPhoneAuth: getApp().globalData.getphone
          })
        }
        // that.getList();
        // that.setData({
        //   display: false
        // })
      } else {
        that.setData({
          display: true
        })
      }

    }, q.detail.encryptedData, q.detail.iv);

  },

  // 取消登录
  cancel_auth: function () {
    this.setData({
      display: false
    })
  },

  onLoad:function(options){
    var e = app.getCache("userinfo");
    var that = this;
    if (!e.nickName && !e.avatarUrl) {
      getApp().authSwitch(function(res){
        if (res == 1) {
          that.setData({
            display: true
          })
        } else {
          that.setData({
            display: false
          })
        }
      })
    }

    this.id = options.id;
    if (options && options.card_id) {
      app.globalData.card_id = options.card_id;
    }
  },

  // 获取用户手机号
  getMobile: function (e) {
    app.globalData.getphone = false;
    this.setData({
      getPhoneAuth: false
    })
    app.getPhoneNumber(e, this, 3);
  },

  /**
   * 回到名片页
   */
  toCard: function () {
    a.jump('/yb_mingpian/pages/card/index', 3);
  },

  onShow: function () {
    var id = this.id;
    var e = app.getCache("userinfo");
    if (e) {
      this.getDetail(id);
      this.setData({
        display: false
      })
    }else{
      this.setData({
        display: true
      })
    }
  },
  getDetail:function(id){
    var self = this; 
    a.get('Pintuan/ptGroupDetail',{
      id: id,
      uid: getApp().getCache("userinfo").uid != undefined ? getApp().getCache("userinfo").uid : 0
    },function(res){
      console.log(res)
        if(res.code==0){
          var timeStr = '';
          if (res.info.leftTime > 0) {
            var t = --res.info.leftTime
            var h = Math.floor(t / 60 / 60)
            var m = Math.floor((t - h * 60 * 60) / 60)
            var s = t % 60;
            if (h < 10) h = "0" + h;
            if (m < 10) m = "0" + m;
            if (s < 10) s = "0" + s;
            timeStr = h + ':' + m + ':' + s
            console.log('a:' + timeStr);
            self.setTimeData(res.info.leftTime, id)
          }
          var groupMember = [];
          for (var i = res.info.goods.groupNum - 1; i >= 0; i--) {
            if (res.info.groupMember[i]) {
              groupMember[i] = res.info.groupMember[i]
            } else {
              groupMember[i] = {}
            }
          }
          self.setData({
            groupMember, groupMember,
            groupInfo: res.info,
            leftTime: timeStr,
            leftnum:res.info.goods.groupNum - res.info.leftNum,
            show:true
          })
        }else{
          a.alert(res.msg);
        }
    },true)
    
  },
  setTimeData:function(time,id){
    var self = this;
    var id = id;
    setInterval(function(){
        var t = --time;
        var h =  Math.floor(t/60/60);
        var m = Math.floor((t-h*60*60)/60);
        var s = t%60;
        if(h<10) h = "0"+h;
        if(m<10) m = "0"+m;
        if(s<10) s = "0"+s;
        var timeStr = h+':'+m+':'+s
        if (h==0&&m==0&&s==0) {
          a.get('Pintuan/ptGroupDetail', {
            id: id
          }, function (res) {
              var groupMember=[];
            for (var i = res.info.goods.groupNum - 1; i >= 0; i--) {
                if (res.info.groupMember[i]){
                  groupMember[i] = res.info.groupMember[i]
                }else{
                  groupMember[i]={}
                }
              }
              self.setData({
                groupMember:groupMember,
                groupInfo: res.info,
              })
            
          })
        }
      self.setData({
        leftTime:timeStr
      })
    }, 1000)
  },
  onShareAppMessage:function(options){
    console.log(options)
    var path = '/yb_mingpian/pages/pintuan/pages/group/detail?id=' + this.data.groupInfo.id + "&card_id=" + app.globalData.card_id
    return {
        title: this.data.groupInfo.goods.name,
        path: path,
        success:function(res){
          console.log(path)
          console.log(res)
        }
      }
  },
  goToHome:function(){
    a.jump('/yb_mingpian/pages/card/index',3)
  },
  showGoodsDetail:function(e){
    var id = e.currentTarget.dataset.id;
    app.redirect('goods/detail','gid='+id)
  },
  goToBuy:function(){
    var goodsDetail = this.data.groupInfo.goods;
    goodsDetail['num'] = this.data.num;
    goodsDetail['goodsPrice'] = this.data.groupInfo.goods.gprice;
    goodsDetail['buyType'] = 1;
    goodsDetail['groupPid'] = this.data.groupInfo.id;
    //console.log(goodsDetail)
    app.goodsInfo = goodsDetail;
    app.redirect('goods/payfor');
  
  },
  selectProp:function(e){
    var current = e.currentTarget.dataset;
    var pid = current.pid;
    var pname = current.pname;
    var name = current.name;
    var propValue = this.propValue ? this.propValue : [];
    propValue[pid] = {pname:pname,name:name};
    this.propValue = propValue;
    this.setData({
      propValue:propValue
    })
  },
  minus:function(){
    var num = this.data.num > 1 ? --this.data.num : 1
    this.setData({
      num : num
    })
  },
  plus:function(){
    var num = ++this.data.num
    this.setData({
      num : num
    })
  },
  showModal:function(e){
   var showModalStatus = e.currentTarget.dataset.statu== 'open' ? true : false;
    app.showModal(this);
    this.setData({
      showModalStatus:showModalStatus
    })
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.onShow();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
})